package cn.k.service.impl;

import cn.k.domain.Employee;
import cn.k.mapper.EmployeeMapper;
import cn.k.qo.QueryObject;
import cn.k.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    @Transactional
    public void insert(Employee employee, Long[] roleIds) {

        Integer age = employee.getAge();
        if (age<18 ||age>60){
            throw new RuntimeException("年龄非法");
        }

        employeeMapper.insert(employee);
         //还要往中间表,employee_role插入给这个员工分配角色数据
        if (roleIds!=null && roleIds.length>0){
            for (Long roleId : roleIds) {
                employeeMapper.insetRelation(employee.getId(),roleId);
            }
        }
    }

    @Override
    public void delete(Long id) {
        //还要从中间表employee_role中删除与这个员工相关的数据
        employeeMapper.deleteRelation(id);
        employeeMapper.deleteByPrimaryKey(id);

    }

    @Override
    @Transactional
    public void update(Employee employee, Long[] roleIds) {
        employeeMapper.updateByPrimaryKey(employee);
        //从中间表employee_role删除旧有的关系数据
        employeeMapper.deleteRelation(employee.getId());
        //再加入新的关系数据
        if (roleIds!=null && roleIds.length>0){
            for (Long roleId : roleIds) {
                employeeMapper.insetRelation(employee.getId(),roleId);
            }
        }

    }

    @Override
    public Employee get(Long id) {
        Employee employee = employeeMapper.selectByPrimaryKey(id);
        return employee;
    }

    @Override
    public List<Employee> selectAll() {
        List<Employee> employees = employeeMapper.selectAll();
        return employees;
    }


    @Override
    public List<Employee> selectForList(QueryObject qo) {
        List<Employee> employees = employeeMapper.selectForList(qo);
        return employees;
    }

    @Override
    public PageInfo<Employee> query(QueryObject qo) {
        //告诉插件我们查询第几页,每页多少条
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        //查询当前页数据
        List<Employee> employees = employeeMapper.selectForList(qo);
        return new PageInfo<>(employees);
    }

    @Override
    public Employee login(String username, String password) {
        return employeeMapper.selectByUsernameAndPassword(username,password);
    }

    @Override
    public boolean checkUsername(String username) {
        Employee employee= employeeMapper.checkUsername(username);
        return employee !=null;
    }

    //导出excel.xls
    @Override
    public Workbook exportXls() {
        //创建excel文件,在内存中
        Workbook wb = new HSSFWorkbook();
        //创建工作簿
        Sheet sheet = wb.createSheet("员工");
        //创建第一行
        Row headRow = sheet.createRow(0);
        //创建第一行第一个单元格,并设置内容
        headRow.createCell(0)
                 .setCellValue("用户名");
        headRow.createCell(1)
                .setCellValue("姓名");
        headRow.createCell(2)
                .setCellValue("邮箱");
        headRow.createCell(3)
                .setCellValue("年龄");


        List<Employee> employees = selectAll();
        for (int i = 0; i < employees.size(); i++) {
            Row row = sheet.createRow(i+1);
            Employee employee = employees.get(i);
            row.createCell(0)
                    .setCellValue(employee.getUsername());
            row.createCell(1)
                    .setCellValue(employee.getName());
            row.createCell(2)
                    .setCellValue(employee.getEmail());
            row.createCell(3)
                    .setCellValue(employee.getAge());

        }
        return wb;
    }

    @Override
    public void importXls(Workbook wb) {
        //拿第一个工作簿
        Sheet sheet = wb.getSheetAt(0);
        //获取这个工作簿最后一行的索引
        int lastRowNum = sheet.getLastRowNum();
        //遍历所有行
        for (int i = 1; i <= lastRowNum; i++) {
            Row row = sheet.getRow(i);
            String username = row.getCell(0).getStringCellValue();
            String name = row.getCell(1).getStringCellValue();
            String email = row.getCell(2).getStringCellValue();
            int age  = (int)row.getCell(3).getNumericCellValue();

            Employee employee = new Employee();
            employee.setUsername(username);
            employee.setName(name);
            employee.setAge(age);
            employee.setEmail(email);

            employeeMapper.insert(employee);
        }

    }

    @Override
    public Employee selectByUsername(String username) {
        return employeeMapper.selectByUsername(username);
    }
}
